using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.Reflection;
using System;
using UnityEngine.Events;
using Unity.VisualScripting;
using UnityEditor.Events;

public class EventObjectEditor<T> : Editor where T : UnityEngine.Object{
    SerializedProperty property;
    T obj;
    Dictionary<string, bool> fold = new Dictionary<string, bool>();
    public override void OnInspectorGUI() {
        obj = (T)target;

        property = serializedObject.GetIterator();
        property.Next(true);
        property.NextVisible(false);//跳过Script属性

        while(property.NextVisible(false)){
            List<string> types = new List<string>(property.tooltip.Split('|'));
            bool enable = true;

            if(types.Exists(t => t.Contains("Disable"))){
                enable = false;
            }

            if(types.Exists(t => t.Contains("Event"))){
                UnityEventBase @event = GetEvent(property.name);
                if(!fold.ContainsKey(property.name))fold.Add(property.name, false);
                if(@event != null){
                    EditorGUILayout.BeginHorizontal();

                    EditorGUILayout.BeginVertical(GUILayout.Width(EditorGUIUtility.labelWidth / 2));
                    
                    EditorGUILayout.PrefixLabel(property.displayName);
                    if(fold[property.name]){
                        for(int i = 0; i<@event.GetPersistentEventCount(); i++){
                            EditorGUILayout.BeginHorizontal();
                            EditorGUILayout.LabelField("",GUILayout.Width(EditorGUIUtility.labelWidth/3*2));
                            if(GUILayout.Button("删除")){
                                UnityEventTools.RemovePersistentListener(@event,i);
                            }
                            EditorGUILayout.EndHorizontal();
                        }
                    }

                    EditorGUILayout.EndVertical();

                    EditorGUILayout.BeginVertical();
                    
                    fold[property.name] = EditorGUILayout.BeginFoldoutHeaderGroup(fold[property.name], "事件列表");
                    if(fold[property.name]){
                        for(int i = 0; i<@event.GetPersistentEventCount(); i++){
                            EditorGUILayout.BeginHorizontal();
                            EditorGUILayout.LabelField(i + ":" + @event.GetPersistentMethodName(i), GUILayout.MinWidth(15));
                            EditorGUILayout.EndHorizontal();
                        }
                    }

                    EditorGUILayout.EndFoldoutHeaderGroup();

                    EditorGUILayout.BeginHorizontal();
                    if(GUILayout.Button("添加")){
                        EventSelectWindow.ShowWindow(@event);
                    }
                    if(GUILayout.Button("清空", GUILayout.MaxWidth(64))){
                        for(int i = @event.GetPersistentEventCount()-1; i>=0; i--){
                            UnityEventTools.RemovePersistentListener(@event,i);
                        }
                    }
                    EditorGUILayout.EndHorizontal();

                    EditorGUILayout.EndVertical();
                    EditorGUILayout.EndHorizontal();
                }
            }else{
                GUI.enabled = enable;
                EditorGUILayout.PropertyField(property);
                GUI.enabled = true;
            }
            
        }

        serializedObject.ApplyModifiedPropertiesWithoutUndo();
        
    }
    public UnityEventBase GetEvent(string name){
        return (UnityEventBase)obj.GetType().GetField(name).GetValue(obj);
    }
}

[CustomEditor(typeof(Dialogue))]
public class DialogueEditor : EventObjectEditor<Dialogue>{}

[CustomEditor(typeof(Effect))]
public class EffectEditor : EventObjectEditor<Effect>{}

[CustomEditor(typeof(Item))]
public class ItemEditor : EventObjectEditor<Item>{}

[CustomEditor(typeof(Weapon))]
public class WeaponEditor : EventObjectEditor<Weapon>{}
